﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-4-20 9:52:34
 ******************************************************************************/

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.Utilities;

    /// <summary>
    /// PiUserManager
    /// 用户管理
    /// </summary>
    public partial class PiUserManager:DbCommonManager
    {
        #region public void BeforeAdd(PiUserEntity userEntity, out string statusCode) 用户添加之前执行的方法
        /// <summary>
        /// 用户添加之前执行的方法
        /// </summary>
        /// <param name="userEntity">用户实体</param>
        /// <param name="statusCode">状态码</param>
        public void BeforeAdd(PiUserEntity userEntity, out string statusCode)
        {
            if (this.Exists(PiUserTable.FieldUserName, userEntity.UserName, PiUserTable.FieldDeleteMark, "0") || DbCommonLibary.Exists(DBProvider, PiStaffTable.TableName, PiStaffTable.FieldUserName, userEntity.UserName, PiStaffTable.FieldDeleteMark, "0"))
            {
                // 用户名已重复
                statusCode = StatusCode.ErrorUserExist.ToString();
            }
            else
            {
                // 检查编号是否重复
                if (!string.IsNullOrEmpty(userEntity.Code) && this.Exists(PiUserTable.FieldCode, userEntity.Code, PiUserTable.FieldDeleteMark, "0"))
                {
                    // 编号已重复
                    statusCode = StatusCode.ErrorCodeExist.ToString();
                }
                else
                {
                    if (!string.IsNullOrEmpty(userEntity.Code) && DbCommonLibary.Exists(DBProvider, PiStaffTable.TableName, PiStaffTable.FieldCode, userEntity.Code))
                    {
                        // 编号已重复
                        statusCode = StatusCode.ErrorCodeExist.ToString();
                    }
                    else
                    {
                        // 检测成功，可以添加用户
                        statusCode = StatusCode.OK.ToString();
                    }
                }
            }
        }
        #endregion

        #region public string Add(PiUserEntity userEntity, out string statusCode) 添加用户
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userEntity">用户实体</param>
        /// <param name="statusCode">状态码</param>
        /// <returns>主键</returns>
        public string Add(PiUserEntity userEntity, out string statusCode)
        {
            string returnValue = string.Empty;
            this.BeforeAdd(userEntity, out statusCode);
            if (statusCode == StatusCode.OK.ToString())
            {
                returnValue = this.AddEntity(userEntity);
                this.AfterAdd(userEntity, out statusCode);
            }
            return returnValue;
        }
        #endregion

        #region public void AfterAdd(BaseUserEntity userEntity, out string statusCode) 用户添加后执行的方法
        /// <summary>
        /// 用户添加之后执行的方法
        /// </summary>
        /// <param name="userEntity">用户实体</param>
        /// <param name="statusCode">状态码</param>
        public void AfterAdd(PiUserEntity userEntity, out string statusCode)
        {
            // 运行成功
            statusCode = StatusCode.OKAdd.ToString();
        }
        #endregion
    }
}
